export class CopyMgr {
    public static copyByH5(copyText: string){
        let textArea = document.createElement("textarea");
        textArea.style.position = 'fixed';
        textArea.style.display = 'opacity';
        textArea.style.top = '0';
        textArea.style.left = '-222';
        textArea.style.width = '2em';
        textArea.style.height = '2em';
        textArea.style.padding = '0';
        textArea.style.border = 'none';
        textArea.style.outline = 'none';
        textArea.style.boxShadow = 'none';
        // textArea.style.background = 'transparent';
        textArea.value = copyText;
        document.body.appendChild(textArea);
        textArea.select();
        // ios必须先选中文字且不支持 input.select(); https://segmentfault.com/a/1190000019525962
        console.log("开始复制")
        // this.selectText(textArea, 0, copyText.length);
        try {
            let success = document.execCommand('copy');
            return success
        } catch (err) {
            // this.sendNotification(Notice.COMMON_TOAST_MSG, '您的平台不支持点击复制到剪贴板');
            return false
        } finally {
            document.body.removeChild(textArea);
        }
    }

    private static selectText(textbox, startIndex, stopIndex) {
        if (textbox.createTextRange) { //ie
            const range = textbox.createTextRange();
            range.collapse(true);
            range.moveStart('character', startIndex); //起始光标
            range.moveEnd('character', stopIndex - startIndex); //结束光标
            range.select(); //不兼容苹果
        } else { //firefox/chrome
            textbox.setSelectionRange(startIndex, stopIndex);
            textbox.focus();
        }
    }

    private static async readText(): Promise<[boolean, string]> {

        try {
            const text = await navigator.clipboard.readText();
            console.log('Pasted content: ', text);
            return Promise.resolve([true, text]);
        } catch (err) {
            console.error('Failed to read clipboard contents: ', err);
            return Promise.resolve([false, ""]);
        }

    }
}